package com.zytech.zspider.eventbus.support;

import com.zytech.modules.lang.Requires;
import com.zytech.zspider.eventbus.EventBus;
import com.zytech.zspider.eventbus.EventEnvelope;
import com.zytech.zspider.eventbus.EventListener;

/**
 * @author siuming
 */
public abstract class AbstractEventBus implements EventBus {

    private final EventOperations eventOperations;
    private final EventListenerContainer listenerContainer;

    /**
     * @param eventOperations
     * @param listenerContainer
     */
    protected AbstractEventBus(EventOperations eventOperations, EventListenerContainer listenerContainer) {
        Requires.notNull(eventOperations);
        Requires.notNull(listenerContainer);
        this.eventOperations = eventOperations;
        this.listenerContainer = listenerContainer;
    }

    @Override
    public final void postQueue(String qname, EventEnvelope envelope) {
        Requires.hasText(qname, "qname must not be null or empty.");
        Requires.notNull(envelope, "eventEnvelope must not be null.");
        eventOperations.sendToQueue(qname, envelope);
    }

    @Override
    public final void postTopic(String tname, EventEnvelope envelope) {
        Requires.hasText(tname, "tname must not be null or empty.");
        Requires.notNull(envelope, "eventEnvelope must not be null.");
        eventOperations.sendToTopic(tname, envelope);
    }

    @Override
    public final void consumeQueue(String qname, EventListener listener) {
        Requires.hasText(qname, "qname must not be null or empty.");
        Requires.notNull(listener, "listener must not be null.");
        listenerContainer.addQueueListener(qname, listener);
    }

    @Override
    public final void consumeTopic(String tname, EventListener listener) {
        Requires.hasText(tname, "tname must not be null or empty.");
        Requires.notNull(listener, "listener must not be null.");
        listenerContainer.addTopicListener(tname, listener);
    }
}
